// pages/tabbar/work/index.js
import { WorkModel } from "../../../api/api-work/workModel.js";
import { CommonModel } from "../../../api/api-common/commonModel.js";
import { config } from "../../../utils/config.js";
import Toast from '@vant/weapp/toast/toast';
import tabService from "../../../utils/tab-service.js";
const workModel = new WorkModel();
const commonModel = new CommonModel();
const App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDialog: false, // 未登录弹窗显示
    orgList: [], // 机构列表
    activeOrgValue: undefined, // 选中机构列表
    // cellList: [],
    cellList: [
    ],
    alerts: "0",
    incomplete: "0"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    // 通过 getTabBar 接口获取组件实例，并调用 setData 更新选中态
    const token = wx.getStorageSync('token')
    if (!token) {
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          list:
            [{
              "pagePath": "/pages/tabbar/work/index",
              "text": "工作",
              "name": "work",
              "iconPath": "/images/tabbar/work.png",
              "selectedIconPath": "/images/tabbar/work-active.png"
            },
            {
              "pagePath": "/pages/tabbar/my/index",
              "text": "我的",
              "name": "my",
              "iconPath": "/images/tabbar/my.png",
              "selectedIconPath": "/images/tabbar/my-active.png"
            }]
        })
        // 设置默认的菜单
        this.setData({
          alerts: 0,
          incomplete: 0,
          cellList: [{
            title: "教务中心",
            children: [
              {
                id: "23",
                icon: `/images/work/dianming.png`,
                title: "点名",
                path: ""
              },
              {
                id: "24",
                icon: `/images/work/jilu.png`,
                title: "上课记录",
                path: "/pages/operation/classRecord/classRecordIndex/index"
              },
            ]
          },]
        })
      }

    }
    else {
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          list: wx.getStorageSync('tabbarList')
        })
      }
      const arr = await tabService.getRoutesByName(App.globalData.activeTab)
      this.setData({
        cellList: arr
      })
      this.getCont()
    }
    this.getTabBar().setData({ active: App.globalData.activeTab })

  },

  getCont() {
    workModel.getContApi().then(res => {
      this.setData({
        alerts: res.count.alerts,
        incomplete: res.count.incomplete
      })
    })
  },

  // 切换机构
  async changeOrgItemClick(e) {
    commonModel.getInfoApi({ orgId: wx.getStorageSync('LoginDeptAuth') }).then(async info => {
      wx.removeStorageSync('permissions')
      // 权限存储
      wx.setStorageSync('permissions', info.permissions)
      // 用户信息存储
      wx.setStorageSync('userInfo', info.user);
      // 用户角色存储
      wx.setStorageSync('roles', info.roles);
      // tabbar存储
      const arr = tabService.changeTabbarList(info.list);
      wx.setStorageSync('tabbarList', arr);

      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          list: wx.getStorageSync('tabbarList')
        })
      }
      this.getTabBar().setData({ active: App.globalData.activeTab })
      // 第六步：获取对应机构的菜单树
      const array = await tabService.getRoutesByName("work")
      this.getCont()
      this.setData({
        cellList: array
      })
    })
  },

  // 跳转
  gotoMenu(e) {
    const token = wx.getStorageSync('token')
    if (!token) {
      // 没有token,去登录
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
    else {
      // 传递的参数
      const url = e.currentTarget.dataset.query.path;
      wx.navigateTo({
        url: url,
      })
    }
  },

  // 续费提醒
  navigateToAlerts() {
    wx.navigateTo({
      url: '/pages/operation/renewalAlert/renewalAlertIndex/index',
    })
  },
  // 超时未点
  navigateToIncomplete() {
    wx.navigateTo({
      url: '/pages/operation/classRecord/classRecordIndex/index',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})